//
//  SellManagerViewController.m
//  ShangPiBao
//
//  Created by chirui on 2017/11/8.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "SellManagerViewController.h"
#import "SellManagerTableViewCell.h"

@interface SellManagerViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    UITableView * _tableView;
    NSMutableArray * _dataArray;
    int _page;
    int _totailPage;
}

@end

@implementation SellManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _page =1;
    _dataArray = @[].mutableCopy;
    TitleView(@"供应信息管理");
    
//    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_return" target:self action:@selector(back)];
    
    [self creatTableView];
    
    [self getDataForRequest];
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:[SellManagerTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[SellManagerTableViewCell reuseIdentifier]];
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _page =1;
        [self getDataForRequest];
        
    }];
    _tableView.mj_header =header;
    MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getDataForRequest];
    }];
    
    footer.automaticallyRefresh = YES;
    _tableView.mj_footer =footer;
    _tableView.mj_footer.hidden = YES;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 130;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = _dataArray[indexPath.row];
    
    SellManagerTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:[SellManagerTableViewCell reuseIdentifier]];
    cell.goodsName.text = [NSString stringWithFormat:@"产品名称：%@",dic[@"ptitle"]];
    cell.goodsDetails.text = [NSString stringWithFormat:@"产品简介：%@",dic[@"psummary"]];
    cell.goodsDetails .numberOfLines =2;
    [cell.goodsDetails sizeToFit];
    [cell.goodsImage sd_setImageWithURL:ImageUrl(dic[@"ppicture"]) placeholderImage:ImageName(@"img_home_newest")];
    cell.deledateBtn.tag = 100+indexPath.row;
    [cell.deledateBtn addTarget:self action:@selector(deledateBtn:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

-(void)deledateBtn:(UIButton *)btn{
    
    NSDictionary * dic = _dataArray[btn.tag-100];
    
    [self deledateForRequestFor:dic[@"pid"]];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = _dataArray[indexPath.row];
    GoodsDetailsViewController * company =[[GoodsDetailsViewController alloc]init];
    company.goodsid = dic[@"pid"];
    [self.navigationController pushViewController:company animated:YES];
    
}


-(void)getDataForRequest{
    
    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_tableView.mj_footer setState: MJRefreshStateNoMoreData];
            
            return ;
        }
    }
    NSString * md5ver = [SHJudgeTools md532BitLower:[NSString
                                                     stringWithFormat:@"%@api.shangpibao.com",USER_DEFAULTGet(@"phone")]];
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@?uphone=%@&ver=%@&uid=%@&page=%d&ustatus=2",address_url,messageManager_list_api,USER_DEFAULTGet(@"phone"),md5ver,USER_DEFAULTGet(@"uid"),_page];
    
    //    NSString *encoded = [parametersStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSLog(@"parametersStr = %@",parametersStr);
    //    SHuiMBhubNoTimeLoding;
    [PostNetWorking netWorkingStr:parametersStr  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"err"] intValue]==0) {
            
            _totailPage = [responseObject[@"maxpage"] intValue];
            
            if (_page==1) {
                
                if (_dataArray) {
                    
                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"data"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"data"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }else {
                
                if ([responseObject[@"data"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"data"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                
                if (_dataArray.count>10) {
                    
                    _tableView.mj_footer.hidden = NO;
                }else {
                    _tableView.mj_footer.hidden = YES;
                    
                }
                [_tableView.mj_header endRefreshing];
                [_tableView.mj_footer endRefreshing];
                [_tableView reloadData];
                
            });
            
        } else {
            
            SHuiMBhudTextTimeStr(EorrMessageStr);
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
            [_tableView reloadData];
            
        });
        
        [_tableView reloadData];
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"请检查您的网络!");
    }];
    
}


-(void)deledateForRequestFor:(NSString *)goodsId{
    
    
    NSString * md5ver = [SHJudgeTools md532BitLower:[NSString
                                                     stringWithFormat:@"%@api.shangpibao.com",USER_DEFAULTGet(@"phone")]];
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@?uphone=%@&ver=%@&upid=%@",address_url,deledate_goods_api,USER_DEFAULTGet(@"phone"),md5ver,goodsId];
    
    NSLog(@"parametersStr = %@",parametersStr);
    //    SHuiMBhubNoTimeLoding;
    [PostNetWorking netWorkingStr:parametersStr  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"err"] intValue]==0) {
            
            _page = 1;
            [self getDataForRequest];
            
        } else {
            
            SHuiMBhudTextTimeStr(EorrMessageStr);
        }
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"请检查您的网络!");
    }];
    
}
@end
